<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ page import="com.yaodian100.core.common.utils.ServerValue"%>
<%@ taglib uri="http://www.yaodian100.com/jsp/yaodian100" prefix="yaodian100"%>
<%@ include file="/taglibs.jsp"%>
 <%
	String path = request.getContextPath();
	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
	response.setHeader("Pragma","No-Cache");  
	response.setHeader("Cache-Control","No-Cache");  
	response.setDateHeader("Expires",   0);   
	%>
	<!-- 月历 -->
	<script type="text/javascript" charset="UTF-8" src="/js/jquery.datepick.js"></script>
	<script type="text/javascript" charset="UTF-8" src="/js/jquery.datepick-zh-CN.js"></script>
	<link rel="stylesheet" type="text/css" href="/css/smoothness.datepick.css"/>
	<!-- 時間 -->	
	<script src="/js/jquery.timePicker.js" type="text/javascript"/></script>
	<link rel="stylesheet" type="text/css" href="/css/timePicker.css"/>
	<script type="text/javascript" charset="UTF-8" src="/js/jquery.alphanumeric.js"></script>
	<script language="javascript">
	var selectArr = new Array("categorySelect","elementSelect","attributeSelect","itemSelect","unitSelect","listSelect");
	$().ready( function() {
		$('#editstartdate').datepick({minDate: null, showOn: 'both', 
	    	buttonImageOnly: true, buttonImage: '<%=ServerValue.getMidContextPath()%>/images/calendar.gif',dateFormat:"yy-mm-dd",yearRange: '-50:+50'});
		$('#editenddate').datepick({minDate: null, showOn: 'both', 
	    	buttonImageOnly: true, buttonImage: '<%=ServerValue.getMidContextPath()%>/images/calendar.gif',dateFormat:"yy-mm-dd",yearRange: '-50:+50'});
		$("#timestart").timePicker();
		$("#timeend").timePicker();
		$("#editshowseq").numeric();
		$("#editname").alphanumeric({ichars:'\'\"'});
		$("#editaliasname").alphanumeric({ichars:'\'\"'});
		<yaodian100:checkPermission permissions="GROSSPROFIT">
		$("#editGrossProfit").numeric({allow:"."});
		</yaodian100:checkPermission>

		  $("#savebutton").click(function () {
	    	if ($("#editname").val() == "" ){
				alert("请输入分类名称");
				return false;
			}
			
	    	if($("#editname").val().length+chineseCount($("#editname").val())>36){
	    		alert("分类名称请使用 中文18个字以内 或 英文36个字以内");
				return false;
		    }
		    if($("#editname").val().match(/,/i)){
	    		alert("分类名称请勿使用半型逗号");
				return false;
		    }
	    	if($("#editaliasname").val().length+chineseCount($("#editaliasname").val())>36){
	    		alert("商品分类别名请使用 中文18个字以内 或 英文36个字以内");
				return false;
		    }
	    	if ($("#editstartdate").val() == "" ){
				alert("请输入开始时间");
				return false;
			}
	    	if ($("#editenddate").val() == "" ){
				alert("请输入结束时间");
				return false;
			}
	    	if ($("#editshowseq").val() == "" ){
				alert("请输入显示顺序");
				return false;
			}
			<c:choose>
		    <c:when test="${not empty newcategory }">
		    	<c:if test="${initlevel ne '1' and initlevel ne '5' and fn:substringAfter(id,'x') ne '11'}">
			    	if ($('#editIsEndNode').is(":checked") == false && $('#editIsEndNode2').is(":checked") == false){
							alert("请输入是否最底层 ");
							return false;
			    	}
					</c:if>
				</c:when>
				<c:otherwise>
					<c:if test="${category.level ne '1' and category.level ne '5' and category.parentId ne '11' and category.isEndnode ne 'Y'}">
			    	if ($('#editIsEndNode').is(":checked") == false && $('#editIsEndNode2').is(":checked") == false){
							alert("请输入是否最底层");
							return false;
			    	}
					</c:if>
				</c:otherwise>
			</c:choose>
	    <c:if test="${empty newcategory }">
	    	if ( $('#editIsEndNode').is(":checked") == true ){
					$.post("<%=ServerValue.getMidContextPath()%>/category/jsonCheckIsEndnode.do", {endNodeId:"${category.id}"}, changeEndnodeCallback, "json");
	    	}else{
	    		$.post("<%=ServerValue.getMidContextPath()%>/category/savedata.do", $('#savedataForm').serialize(), callbackFun, "json");
	    	}
			</c:if>
			<c:if test="${not empty newcategory }">
				$.post("<%=ServerValue.getMidContextPath()%>/category/savedata.do", $('#savedataForm').serialize(), callbackFun, "json");
			</c:if>
	    	
		  }); 
	});
	function changeEndnodeCallback(changeEndnodeMsg){
		if ( null == changeEndnodeMsg ){
			$.post("<%=ServerValue.getMidContextPath()%>/category/savedata.do", $('#savedataForm').serialize(), callbackFun, "json");
		}else{
			var r = confirm(changeEndnodeMsg);
			if (r == true){
				$.post("<%=ServerValue.getMidContextPath()%>/category/savedata.do", $('#savedataForm').serialize(), callbackFun, "json");
			}
			return false;
		}
	}
	function chineseCount(word){
	    var length=0;
	    for(var cc=0;cc<word.length;cc++){
	       var c = word.charCodeAt(cc);
	      if (!(c>=32&&c<=126)) length++;
	    }
	    return length;
	}

	function callbackFun(saveResult){
		if(saveResult.indexOf('error') != -1){
			alert(saveResult);
		}else{
			var spdata = saveResult.split('-');
			if(spdata[0]=='1'){ //新增
				switch(spdata[3]){
				case '1':
					var new_id = $("#elementSelect option:last").val();
					$("#elementSelect option:last").remove();
					$('#elementSelect').html($('#elementSelect').html()+'<option value="'+spdata[1]+'">'+spdata[2]+'</option>'+
							'<option value="'+new_id+'">===新增分类===</option>');
					break;
				case '2':
					var new_id = $("#attributeSelect option:last").val();
					$("#attributeSelect option:last").remove();
					$('#attributeSelect').html($('#attributeSelect').html()+'<option value="'+spdata[1]+'">'+spdata[2]+'</option>'+
							'<option value="'+new_id+'">===新增分类===</option>');
					break;
				case '3':
					var new_id = $("#itemSelect option:last").val();
					$("#itemSelect option:last").remove();
					$('#itemSelect').html($('#itemSelect').html()+'<option value="'+spdata[1]+'">'+spdata[2]+'</option>'+
							'<option value="'+new_id+'">===新增分类===</option>');
					break;
				case '4':
					var new_id = $("#unitSelect option:last").val();
					$("#unitSelect option:last").remove();
					$('#unitSelect').html($('#unitSelect').html()+'<option value="'+spdata[1]+'">'+spdata[2]+'</option>'+
							'<option value="'+new_id+'">===新增分类===</option>');
					break;
				case '5':
					var new_id = $("#listSelect option:last").val();
					$("#listSelect option:last").remove();
					$('#listSelect').html($('#listSelect').html()+'<option value="'+spdata[1]+'">'+spdata[2]+'</option>'+
							'<option value="'+new_id+'">===新增分类===</option>');
					break;
				}
			}
			alert('储存成功');
			setTimeout(function() {reloadCurrentLevel(spdata[1],spdata[3]);},300);
		}
	}
	function removeCategory(level){
		switch(level){
			case 1:
				$('#elementSelect').find('option').remove().end();
				$('#attributeSelect').find('option').remove().end();
		   	$('#itemSelect').find('option').remove().end();
		   	$('#unitSelect').find('option').remove().end();
		   	$('#listSelect').find('option').remove().end();
		   	break;
			case 2:
				$('#attributeSelect').find('option').remove().end();
		   	$('#itemSelect').find('option').remove().end();
		   	$('#unitSelect').find('option').remove().end();
		   	$('#listSelect').find('option').remove().end();
		   	break;
			case 3:
		   	$('#itemSelect').find('option').remove().end();
		   	$('#unitSelect').find('option').remove().end();
		   	$('#listSelect').find('option').remove().end();
		   	break;
			case 4:
		   	$('#unitSelect').find('option').remove().end();
		   	$('#listSelect').find('option').remove().end();
		   	break;
			case 5:
		   	$('#listSelect').find('option').remove().end();
		   	break;
		}
	}
	function reloadCurrentLevel(tmpVal,tmpLevel) {
		var selectObj1 = selectArr[parseInt(tmpLevel,10)-1];
		var selectObj2 = selectArr[tmpLevel];
		var nowval = $('#' + selectObj1).val();
		removeCategory(parseInt(tmpLevel,10));
		if(null!=nowval){
			if(nowval[0]!='x')
			$.ajax({
			    url: '<%=ServerValue.getMidContextPath()%>/category/checkJsonData.do',
			    type: 'post',
			    dataType: 'json',
			    data: {categoryid:nowval, callback: 'selectCallback'},
			    cache: false,
			    beforeSend: function(xhr) {
					$("#loadingImg").dialog("open");
					$('#categorycontent').hide();
			    },
			    success: function( strData ){
			    	viewCallback(strData, selectObj2,tmpVal);
				}
			});		
			$('#categorycontent').load('<%=ServerValue.getMidContextPath()%>/category/editdata.do?id='+$("#" + selectObj1).val());
			$('#categorycontent').show();			   						
		}
		setTimeout(function() {
			reloadNextLevel(tmpVal,parseInt(tmpLevel,10)+1);
		},300);
	}

	function reloadNextLevel(tmpVal,tmpLevel) {
		var selectObj = selectArr[tmpLevel];
		var nowval = tmpVal;
		removeCategory(tmpLevel);
		if(null!=nowval){
			if(nowval[0]!='x')
			$.ajax({
			    url: '<%=ServerValue.getMidContextPath()%>/category/checkJsonData.do',
			    type: 'post',
			    dataType: 'json',
			    data: {categoryid:nowval, callback: 'selectCallback'},
			    cache: false,
			    beforeSend: function(xhr) {
					$("#loadingImg").dialog("open");
					$('#categorycontent').hide();
			    },
			    success: function( strData ){
			    	viewCallback(strData, selectObj);
				}
			});		
			$('#categorycontent').load('<%=ServerValue.getMidContextPath()%>/category/editdata.do?id='+tmpVal);
			$('#categorycontent').show();			   						
		}
	}

	function viewCallback(categories, reloadArea,selTmpVal){
	  	jQuery.each(categories, function(index , value){
	      		var data = value.split(':');
	      		if(data[0].substring(0,1) == "x"){
	      			$('#'+reloadArea).append($("<option></option>").attr("value",data[0]).text(data[1]));
	      		}else{
	          		var cmark='';
	          		if(data[2]=="Y"){
	          			cmark=cmark+'○';//品牌
	          		}
	          		 if(data[3]=="Y"){
	          			 cmark=cmark+'◎';//活動
	          		}
	           		 if(data[4]=="N"){
	           			 cmark=cmark+'※';//是否可見
	          		} 
	              if(data[2]!="Y" && data[3]!="Y" && data[4]!="N"){
	                 if (selTmpVal == data[0]) {
									$('#'+reloadArea).append($("<option selected='true'></option>").attr("value",data[0]).text(data[1]+'('+data[0]+')'));
	                 } else {
	                 	$('#'+reloadArea).append($("<option></option>").attr("value",data[0]).text(data[1]+'('+data[0]+')'));
	                 }
	          		}else{
	          			$('#'+reloadArea).append($("<option></option>").attr("value",data[0]).text(cmark+data[1]+'('+data[0]+')'));
	          		}
	      		}
	      });
	  	$("#categorycontent").ajaxSuccess(function( event,request, settings ){
		   $("#loadingImg").dialog("close");
			});
		}
	function GetCharLength(str)
	{
		var iLength = 0;
		for(var i = 0;i<str.length;i++)
			{
			if(str.charCodeAt(i) >255)
			{
				iLength += 2;
			}
			else
			{
				iLength += 1;
			}
		}
		return iLength;
	}
	function CutStr(Str,Len)
	{
		var CurStr = "";
		for(var i = 0;i<Str.length;i++)
			{
				CurStr += Str.charAt(i);
			if(GetCharLength(CurStr )>Len)
			{
				return Str.substring(0,i);
			}
		}
		return CurStr ;
	} 

</script>
<s:actionerror/>
<s:actionmessage/>
<s:fielderror />
	<s:form id="savedataForm" action="savedata.do" method="post">
	<table>
		<tr><td><span class="txt-rd">※符号表示该分类为隐藏;○表品牌;◎表活動</span></td></tr>
		<tr><td>
			分类编号:
			<s:if test="newcategory == ''">
			${category.id}
			</s:if>
			<br>
		</td></tr><tr><td>
			分类名称:
			<s:if test="newcategory == ''">
			<input type="text" id="editname" name="editname" value="${category.name}" size="20" maxlength="36"/><span class="txt-rd">字数限制:18个中文字,36英文字</span><br>
			</s:if>
			<s:elseif test="newcategory != ''">
			<input type="text" id="editname" name="editname" value="" size="20" maxlength="36"/><span class="txt-rd">字数限制:18个中文字,36英文字</span><br>
			</s:elseif>
		</td></tr>
		<tr><td>
			商品分类别名:
			<s:if test="newcategory == ''">		
			<input type="text" id="editaliasname" name="editaliasname" value="${category.aliasName}" size="20" maxlength="36"/><span class="txt-rd">字数限制:18个中文字,36英文字</span><br>
			</s:if>
			<s:elseif test="newcategory != ''">
			<input type="text" id="editaliasname" name="editaliasname" value="" size="20" maxlength="36"/><span class="txt-rd">字数限制:18个中文字,36英文字</span><br>
			</s:elseif>
		</td></tr>
		<tr><td>	
			父类别:
			<s:if test="newcategory == ''">
			${parentName}(编号:${category.parentId})
			</s:if>
			<s:elseif test="newcategory != ''">
			${parentName}(编号:${newcategory})<input type="hidden" id="newcategory" name="newcategory" value="${newcategory}" maxlength="30"/>
			</s:elseif>
			<br>
		</td></tr><tr><td>	
			开始时间:
			<s:if test="newcategory == ''">
			<input type="text" id="editstartdate" name="editstartdate" value="<s:date name='category.startDate' format='yyyy-MM-dd' />"   size="10" maxlength="10"/>
			<input id="timestart" name="timestart" type="text" value="<fmt:formatDate value="${category.startDate}" pattern="HH:mm" />"   size="10" /><br>
			</s:if>
			<s:elseif test="newcategory != ''">
			<input type="text" id="editstartdate" name="editstartdate" value="${editstartdate}" size="10" maxlength="10"/>
			<input id="timestart" name="timestart" type="text" size="10" value="00:00"/>ex:2009-12-31 00:00<br>
			</s:elseif>				
		</td></tr><tr><td>	
			结束时间:
			<s:if test="newcategory == ''">
			<input type="text" id="editenddate" name="editenddate" value="<s:date name='category.endDate' format='yyyy-MM-dd' />" size="10" maxlength="10"/>
			 <input id="timeend" name="timeend" type="text" value="<fmt:formatDate value="${category.endDate}" pattern="HH:mm" />" size="10" /><br>
			</s:if>
			<s:elseif test="newcategory != ''">
			<input type="text" id="editenddate" name="editenddate" value=""  size="10" maxlength="10"/>
			<input id="timeend" name="timeend" type="text" size="10" value="00:00"/>ex:2009-12-31 00:00<br>
			</s:elseif>		
		</td></tr><tr><td>	
			显示顺序:
			<s:if test="newcategory == ''">
			<input type="text" id="editshowseq" name="editshowseq" value="${category.showSeq}" maxlength="3" size="3"/>
			</s:if>
			<s:elseif test="newcategory != ''">
			<input type="text" id="editshowseq" name="editshowseq" value="" maxlength="3" size="3"/>
			</s:elseif>
			<BR>
		</td></tr>
		<c:if test="${initlevel ne '1' or category.level ne '1'}">
		<tr><td>
			是否设为活动特辑:	
			<s:if test="newcategory == ''">	
			<input type="radio" id="editispromotion" name="editispromotion" value="Y" <c:if test="${category.isPromotion eq 'Y'}">checked="checked"</c:if>/>是
			<input type="radio" id="editispromotion" name="editispromotion" value="N" <c:if test="${category.isPromotion eq 'N'}">checked="checked"</c:if>/>否	<span class="txt-rd">「注意：在分类1无显示效果」</span>	
			</s:if>
			<s:elseif test="newcategory != ''">
			<input type="radio" id="editispromotion" name="editispromotion" value="Y" />是
			<input type="radio" id="editispromotion" name="editispromotion" value="N" checked="checked" />否 <span class="txt-rd">「注意：在分类1无显示效果」</span>
			</s:elseif>
			<br>
		</td></tr>
		<tr><td>	
			是否设为品牌分类:
			<s:if test="newcategory == ''">	
			<input type="radio" id="editisbrand" name="editisbrand" value="Y" <c:if test="${category.isBrand eq 'Y'}">checked="checked"</c:if>/>是
			<input type="radio" id="editisbrand" name="editisbrand" value="N" <c:if test="${category.isBrand eq 'N'}">checked="checked"</c:if>/>否<span class="txt-rd">「注意：在分类1无显示效果」</span>		
			</s:if>
			<s:elseif test="newcategory != ''">
			<input type="radio" id="editisbrand" name="editisbrand" value="Y" />是
			<input type="radio" id="editisbrand" name="editisbrand" value="N" checked="checked" />否<span class="txt-rd">「注意：在分类1无显示效果」</span>
			</s:elseif>
			<br>
		</td></tr>
		</c:if>	
		<tr><td>	
			字体是否强化效果:
			<s:if test="newcategory == ''">
			<input type="radio" id="editishighlight" name="editishighlight" value="Y" <c:if test="${category.isHighlight eq 'Y'}">checked="checked"</c:if>/>是
			<input type="radio" id="editishighlight" name="editishighlight" value="N" <c:if test="${category.isHighlight eq 'N'}">checked="checked"</c:if>/>否		
			</s:if>
			<s:elseif test="newcategory != ''">
			<input type="radio" id="editishighlight" name="editishighlight" value="Y" />是
			<input type="radio" id="editishighlight" name="editishighlight" value="N" checked="checked" />否
			</s:elseif> *前台为以红色加强显示
			<br>
		</td></tr>	
		<tr><td>	
			是否可见:
			<s:if test="newcategory == ''">
			<input type="radio" id="editisvisible" name="editisvisible" value="Y" <c:if test="${category.isVisible eq 'Y'}">checked="checked"</c:if>/>是
			<input type="radio" id="editisvisible" name="editisvisible" value="N" <c:if test="${category.isVisible eq 'N'}">checked="checked"</c:if>/>否
			</s:if>
			<s:elseif test="newcategory != ''">
			<input type="radio" id="editisvisible" name="editisvisible" value="Y" checked="checked" />是
			<input type="radio" id="editisvisible" name="editisvisible" value="N" />否
			</s:elseif>
			<br>
		</td></tr>
		<tr><td>	
			部门:
			<s:if test="newcategory == ''">${category.orgnizationPath}</s:if>
			<s:elseif test="newcategory != ''">${editorgnizationpath}</s:elseif>
		</td></tr>
		<tr><td>
			 层级:
			<s:if test="newcategory == ''">${category.level}</s:if>
			<s:elseif test="newcategory != ''">${initlevel}</s:elseif>
		</td></tr>
		<tr><td>	
			是否最底层:
			<s:if test="newcategory == ''">
				<c:choose>
					<c:when test="${(category.level eq '1' and category.parentId eq '11') or category.level eq '5' or category.isEndnode eq 'Y'}">是</c:when>
					<c:when test="${category.level eq '1' and category.parentId ne '11'}">否</c:when>
					<c:otherwise>
						<input type="radio" id="editIsEndNode" name="editIsEndNode" value="Y" <c:if test="${category.isEndnode eq 'Y'}">checked="checked"</c:if>/>是
						<input type="radio" id="editIsEndNode2" name="editIsEndNode" value="N" <c:if test="${category.isEndnode eq 'N'}">checked="checked"</c:if>/>否
					</c:otherwise>
				</c:choose>
				<input type="hidden" id="flagIsEndNodeForSearchBox" value="${category.isEndnode}"/>
				<input type="hidden" id="flagIsEndNodeForSearchBoxParentId" value="${category.id}"/>
			</s:if>
			<s:elseif test="newcategory != ''">
				<c:choose>
					<c:when test="${(initlevel eq '1' and  fn:substringAfter(id,'x') eq '11' ) or initlevel eq '5'}">是</c:when>
					<c:when test="${initlevel eq '1' and fn:substringAfter(id,'x') ne '11'}">否</c:when>
					<c:otherwise>
						<input type="radio" id="editIsEndNode" name="editIsEndNode" value="Y" checked="checked"/>是
						<input type="radio" id="editIsEndNode2" name="editIsEndNode" value="N"/>否
					</c:otherwise>
				</c:choose>
			</s:elseif>
			<BR>
		</td></tr><tr><td>	
			版型编号:
			<s:select id="templatexxx" name="edittemplateid"  value="category.templateId" list="menu.options" listKey="key" listValue="value.name" /> 
		</td></tr>
		<tr><td>	
		<s:if test="newcategory == ''">
		建立者:${category.createUser}<br/>
		建立日期:<s:date name='category.createDate' format='yyyy-MM-dd HH:mm' /><br/>
		修改者:${category.modifyUser}<br/>
		修改日期:<s:date name='category.modifyDate' format='yyyy-MM-dd HH:mm' /><br/>
		</s:if>
		</td></tr>
		<yaodian100:checkPermission permissions="GROSSPROFIT">
		<fmt-rt:formatNumber value="${category.grossProfit}" pattern="#####0.0" var="grossProfit"/>
		<tr><td>毛利率:<input id="editGrossProfit" name="editGrossProfit" size="3" type="text" value="${grossProfit}"/>%</td></tr>
		</yaodian100:checkPermission>
		</table>
	<s:param name="id" value="#category.id"/>
	
	<input type="hidden" name="id" value="${category.id}"/>
		<input id="savebutton" type="button" value="Save" />
	</s:form>